//
//  MenusViewController.m
//  CarpoolWithFriends
//
//  Created by Kai Wang on 5/6/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "MenusViewController.h"
#import "RevealController.h"
#import "StreamFirstLevelViewController.h"
#import "SearchFirstLevelViewController.h"
#import "FriendsFirstLevelViewController.h"
#import "MyProfileFirstLevelViewController.h"
#import "SettingFirstLevelViewController.h"
#import "NotificationViewController.h"
#import "PostStepOneViewController.h"
#import "LoginViewController.h"
#import "AccountUtil.h"
#import "CarpoolWithFriendsAdapter.h"

@interface MenusViewController()
//private properties

// Pirvate methods
-(void)revealMessageView:(id)sender;
-(void)revealSearchView:(id)sender;
@end

@implementation MenusViewController
@synthesize menuTableView;
@synthesize toolBar;

#pragma marl - UITableView Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
    }    
    NSString *title = [[NSString alloc]init];
    switch (indexPath.row) {
        case 0:
            title = NSLocalizedString(@"STREAM", nil);
            break;
        case 1:
            title = @"New Post";
            break;
        case 2:
            title = @"Profile";
            break;
        case 3:
            title = @"Friends";
            break;
        case 4:
            title = @"Setting";
            break;
        case 5:
            title = @"Logout";
            break;
        default:
            break;
    }
    
    cell.textLabel.text = title;
    cell.textLabel.shadowColor = [UIColor blackColor];
    cell.textLabel.shadowOffset = CGSizeMake(0.0,-2.0);
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_tableview_cell.png"]];
    cell.backgroundView = bg;
    
    [bg release];
    [title release];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	// Grab a handle to the reveal controller, as if you'd do with a navigtion controller via self.navigationController.
	RevealController *revealController = [self.parentViewController isKindOfClass:[RevealController class]] ? (RevealController *)self.parentViewController : nil;
	UIViewController *viewController;
    switch (indexPath.row) {
        case 0:
            viewController = [[StreamFirstLevelViewController alloc] init];
            break;
        case 1:
            viewController = [[PostStepOneViewController alloc] init];
            break;
        case 2:
            viewController = [[MyProfileFirstLevelViewController alloc] init];
            break;
        case 3:
            viewController =[[FriendsFirstLevelViewController alloc] init];
            break;
        case 4:
            viewController = [[SettingFirstLevelViewController alloc] init];
            break;
        case 5:
            // TODO: clean local record
            [AccountUtil resetUserDefault];
            
            // send transaction to server
//            [CarpoolWithFriendsAdapter requestLogoutWithUserId:<#(NSString *)#> retData:<#(NSMutableDictionary **)#>];
            
            
            viewController = [[LoginViewController alloc]init];
            break;   
        default:
            break;
    }
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [viewController release];
    [revealController setFrontViewController:navigationController animated:YES];
    [navigationController release];    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [menuTableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.menuTableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_tableview_cell_background.png"]];
    //    self.menuTableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button_blue.png"]];
    self.menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidUnload
{
    self.menuTableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // Create the array to hold the buttons, which then gets added to the toolbar
    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity: 3];
    
    UIBarButtonItem *aBarButtonItem;
    UIImage *aButtonImage;
    UIButton *aButton;
    
    // create a message button
    aButtonImage = [UIImage imageNamed:@"message.png"];
    aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setImage:aButtonImage forState:UIControlStateNormal];
    aButton.frame = CGRectMake(0.0, 0.0, aButtonImage.size.width, aButtonImage.size.height);
    aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    [aButton addTarget:self action:@selector(revealMessageView:) forControlEvents:UIControlEventTouchUpInside];
    [buttons addObject:aBarButtonItem];
    
    [aButtonImage release];
    [aButton release];
    [aBarButtonItem release];
    
    // Create a spacer
    aBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [buttons addObject:aBarButtonItem];
    [aBarButtonItem release];
    
    // Create a standard "Search" button
    aBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(revealSearchView:)];
    [buttons addObject:aBarButtonItem];
    [aBarButtonItem release];
    
    // stick the buttons in the toolbar
    [toolBar setItems:buttons animated:NO];
    
    [buttons release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)revealMessageView:(id)sender
{
    RevealController *revealController = [self.parentViewController isKindOfClass:[RevealController class]] ? (RevealController *)self.parentViewController : nil;
    NotificationViewController *viewController = [[NotificationViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [viewController release];
    [revealController setFrontViewController:navigationController animated:YES];
    [navigationController release];
}

-(void)revealSearchView:(id)sender
{
    RevealController *revealController = [self.parentViewController isKindOfClass:[RevealController class]] ? (RevealController *)self.parentViewController : nil;
    SearchFirstLevelViewController *viewController = [[SearchFirstLevelViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [viewController release];
    [revealController setFrontViewController:navigationController animated:YES];
    [navigationController release];
}

@end
